class ShareController {

    private commonCount;
    private map : any = {}
    
    constructor() {
        this.commonCount = Cfg.ShareCfg["common_count"]["value"];
    }
    
    shareApp() : void{
        if (Laya.Browser.onWeiXin) {
            wx.onShow(this.showApp);
            wx.onShareAppMessage(this.shareCommon);
        }
    }

    //进行普通分享(分享结果目前算作地块的邀请)
    shareCommon() : void{
        let imgIndex = Math.round(Math.random() * (this.commonCount-1)) + 5000;
        if (Laya.Browser.onWeiXin) {
            wx.shareAppMessage({
                title: Cfg.ShareCfg[imgIndex]["content"],
                query: "invite_id="+KBEngine.app.player().dbid+"&share_type=" + Cfg.ShareCfg["share_type_field"].value,
                imageUrl: ServerCfg.ResUrl+"share/common/"+Cfg.ShareCfg[imgIndex]["skin"],
                trigger: function (res) {
                    console.log("shareCommon trigger ==>> res : ",res);
                },
                success: function (res) {
                    console.log("shareCommon success ==>> res : ",res);
                    ComFun.BaseCall("shareComplete");
                },
                cancel: function (res) {
                    console.log("shareCommon cancel ==>> res : ",res);
                },
                fail: function (res) {
                    console.log("shareCommon fail ==>> res : ",res);
                }
            });
        }
    }

    //进行特定功能分享
    shareSpecify(shareType : number) : void{
        let share = null;
        for (let key in Cfg.ShareCfg) {
            if(Cfg.ShareCfg[key]["type"] == shareType){
                share = Cfg.ShareCfg[key];
                break;
            }
        }
        if(share == null) return;
        if (Laya.Browser.onWeiXin) {
            wx.shareAppMessage({
                title: share["content"],
                query: "invite_id="+KBEngine.app.player().dbid+"&share_type="+shareType,
                imageUrl: ServerCfg.ResUrl+"share/common/" + share["skin"],
                trigger: function (res) {
                    console.log("shareSpecify trigger ==>> res : ",res);
                },
                success: function (res) {
                    console.log("shareSpecify success ==>> res : ",res);
                    ComFun.BaseCall("shareComplete");
                },
                cancel: function (res) {
                    console.log("shareSpecify cancel ==>> res : ",res);
                },
                fail: function (res) {
                    console.log("shareSpecify fail ==>> res : ",res);
                }
            });
        }
    }

    //进入app,若点击链接进入,则会携带query参数
    showApp(res) : void{
        if(!res) return;
        console.log("showApp ==>> res : ", res)
        console.log("showApp ==>> scene : ", res.scene);
        console.log("showApp ==>> query : ", res.query);
        console.log("showApp ==>> shareTicket : ", res.shareTicket);
        console.log("showApp ==>> referrerInfo : ", res.referrerInfo);
        if(res.query.invite_id){
            if(res.query.invite_id == KBEngine.app.player().dbid) return;
            ComFun.BaseCall("shareClick", res.query.invite_id, res.query.share_type);
            //Laya.timer.once(10000, this, this.showApp, [res]);
            window.clearTimeout(this.map[res.query.invite_id.toString + res.query.share_type]);
            this.map[res.query.invite_id.toString + res.query.share_type] = window.setTimeout(this.showApp, 10000,[res]);
            console.log("showApp ==>> this.map : ", this.map);
        }
    }

    //点击分享链接回调,服务器调用
    onShareClick(dbid:string, shareType:string){
        console.log("showApp ==>> dbid : ", dbid, ", shareType", shareType);
        window.clearTimeout(this.map[dbid+shareType]);
    }

}